package com.business_clean.ui.activity.custom;

import androidx.annotation.NonNull;
import androidx.lifecycle.Observer;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.os.Bundle;
import android.view.View;

import com.blankj.utilcode.util.SizeUtils;
import com.business_clean.app.App;
import com.business_clean.app.base.BaseActivity;
import com.business_clean.app.uitl.ActivityForwardUtil;
import com.business_clean.app.uitl.CheckListDataUtil;
import com.business_clean.app.weight.EqualSpacingItemDecoration;
import com.business_clean.data.mode.custom.CustomMangerEntity;
import com.business_clean.databinding.ActivityCustomManagerBinding;
import com.business_clean.ui.adapter.custom.CustomMangerAdapter;
import com.business_clean.viewmodel.request.CustomViewModel;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import static com.business_clean.app.config.Constant.TYPE_INIT_DATA;
import static com.business_clean.app.config.Constant.TYPE_INIT_LOAD_MORE;

public class CustomManagerActivity extends BaseActivity<CustomViewModel, ActivityCustomManagerBinding> {

    private CustomMangerAdapter mAdapter;

    private int page = 1;
    private int requestType;

    private boolean isSelected = false;

    @Override
    public void initView(@Nullable Bundle savedInstanceState) {

        if (getIntent() != null && getIntent().getExtras() != null) {
            isSelected = getIntent().getExtras().getBoolean("isSelected");
        }

        mAdapter = new CustomMangerAdapter();

        mDatabind.list.recyclerView.setLayoutManager(new LinearLayoutManager(this));
        mDatabind.list.recyclerView.addItemDecoration(new EqualSpacingItemDecoration(SizeUtils.dp2px(10)));
        mDatabind.list.recyclerView.setAdapter(mAdapter);

        mDatabind.bottom.butBase.setText("+ 添加客户");
//        mDatabind.tabbar.setRightIcon(R.mipmap.icon_base_add);
        mDatabind.list.refreshLayout.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull @NotNull RefreshLayout refreshLayout) {
                requestMore();
            }

            @Override
            public void onRefresh(@NonNull @NotNull RefreshLayout refreshLayout) {
                requestOne();
            }
        });
        requestOne();
    }

    @Override
    public void onBindViewClick() {
        mAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull @NotNull BaseQuickAdapter<?, ?> adapter, @NonNull @NotNull View view, int position) {
                if (isSelected) {
                    App.appViewModelInstance.getSelectCustomMangerList().setValue(mAdapter.getData().get(position));
                    finish();
                    return;
                }
                Bundle bundle = new Bundle();
                bundle.putSerializable("custom", mAdapter.getData().get(position));
                ActivityForwardUtil.startActivity(CustomDetailActivity.class, bundle);
            }
        });

        mDatabind.bottom.butBase.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ActivityForwardUtil.startActivity(AddCustomActivity.class);
            }
        });
    }

    @Override
    public void initObserver() {
        mViewModel.getCustomMangerList().observe(this, new Observer<CustomMangerEntity>() {
            @Override
            public void onChanged(CustomMangerEntity customMangerEntity) {
                switch (requestType) {
                    case TYPE_INIT_DATA:
                        // 根据数据来决定要显示哪一个状态的View
                        if (CheckListDataUtil.checkInitData(customMangerEntity.getList(), mAdapter, getMDatabind().list.refreshLayout)) {
                            mAdapter.setList(customMangerEntity.getList());
                        }
                        break;
                    case TYPE_INIT_LOAD_MORE:
                        // 根据数据来决定要显示哪一个状态的View
                        if (CheckListDataUtil.checkLoadMoreData(customMangerEntity.getList(), mAdapter, getMDatabind().list.refreshLayout)) {
                            mAdapter.addData(customMangerEntity.getList());
                        }
                        break;
                }
            }
        });

        App.appViewModelInstance.getRefreshCustomManagerList().observe(this, new Observer<Boolean>() {
            @Override
            public void onChanged(Boolean aBoolean) {
                requestOne();
            }
        });
    }

    private void requestOne() {
        requestType = TYPE_INIT_DATA;
        page = 1;
        mViewModel.getCustomList(page);
    }

    private void requestMore() {
        requestType = TYPE_INIT_LOAD_MORE;
        page++;
        mViewModel.getCustomList(page);
    }
}